package com.axter.libs.photo.previewphoto;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.axter.libs.photo.ImageLoaderCache;
import com.axter.libs.photo.R;
import com.axter.libs.photo.display.IPhotoDisplayInterface;
import com.axter.libs.photo.display.SimplePhotoDisplay;
import com.axter.libs.photo.widget.HackyViewPager;

import java.util.ArrayList;

import pl.droidsonroids.gif.GifDrawable;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


/**
 * 查看图片详情
 *
 * @author axter
 */
public class PreviewPhotoBaseActivity extends Activity implements View.OnClickListener, PhotoViewAttacher.OnPhotoTapListener {
    public static final String EXTRA_IMAGE_PATHS = "image_paths";
    public static final String EXTRA_POSITION = "image_pos";
    public static final String EXTRA_CHECK = "image_check";
    public static final String EXTRA_OUT_DEL_POSITION = "image_del_position";
    public static final String EXTRA_OUT_CHECK = "image_check";
    public static final String EXTRA_DISPLAY_INTERFACE = "display_interface";
    public static final int EXTRA_TYPE_PREVIEW = 1;
    public static final int EXTRA_TYPE_DEL = 2;
    public static final int EXTRA_TYPE_CHECK = 3;
    protected HackyViewPager mViewPager;
    protected PreviewPhotoAdapter mAdapter;
    private String photoDisplayInterfacePath;
    private IPhotoDisplayInterface photoDisplayInterface;
    /**
     * 当前page页坐标
     */
    protected int mCurrentIndex;
    /**
     * 图片地址
     */
    protected ArrayList<String> mPaths;
    private boolean isFinish = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        receiveIntentArgs();

        if (isFinish) {
            return;
        }

        setContentView(getContentView());

        initView();

        initData();
    }

    protected void receiveIntentArgs() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            mCurrentIndex = getIntent().getExtras().getInt(EXTRA_POSITION, 0);
            mPaths = getIntent().getExtras().getStringArrayList(EXTRA_IMAGE_PATHS);
        }
        if (getIntent().getExtras().containsKey(EXTRA_DISPLAY_INTERFACE)) {
            photoDisplayInterfacePath = getIntent().getExtras().getString(EXTRA_DISPLAY_INTERFACE);
        }
        if (mPaths == null || mPaths.size() == 0) {
            Toast.makeText(this, R.string.photoselect_no_pictures_to_preview, Toast.LENGTH_SHORT).show();
            super.finish();
            isFinish = true;
        }
    }

    protected int getContentView() {
        return R.layout.previewphoto;
    }

    private void setDisplayInterface() {
        try {
            if (!TextUtils.isEmpty(photoDisplayInterfacePath)) {
                if (photoDisplayInterface != null && photoDisplayInterface.getClass().getName().equals(photoDisplayInterfacePath)) {
                    return;
                }
                Class clazz = Class.forName(photoDisplayInterfacePath);
                photoDisplayInterface = (IPhotoDisplayInterface) clazz.newInstance();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (photoDisplayInterface == null) {
            photoDisplayInterface = new SimplePhotoDisplay();
        }
        ImageLoaderCache.setDisplayInterface(photoDisplayInterface);
    }

    protected void initView() {
        // 页面数据
        mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
        mAdapter = new PreviewPhotoAdapter(this, mPaths);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(mCurrentIndex);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mCurrentIndex = position;
                setPageText(mCurrentIndex);
                try {
                    PhotoView photoView = PreviewPhotoAdapter.getView(mViewPager, position);
                    if (photoView != null) {
                        Drawable drawable = photoView.getDrawable();
                        if (drawable != null && drawable instanceof GifDrawable) {
                            ((GifDrawable) drawable).start();
                        }
                    }
                } catch (Exception ex) {

                }
                try {
                    PhotoView photoView = PreviewPhotoAdapter.getView(mViewPager, position - 1);
                    if (photoView != null) {
                        Drawable drawable = photoView.getDrawable();
                        if (drawable != null && drawable instanceof GifDrawable) {
                            ((GifDrawable) drawable).stop();
                        }
                    }
                } catch (Exception ex) {

                }
                try {
                    PhotoView photoView = PreviewPhotoAdapter.getView(mViewPager, position + 1);
                    if (photoView != null) {
                        Drawable drawable = photoView.getDrawable();
                        if (drawable != null && drawable instanceof GifDrawable) {
                            ((GifDrawable) drawable).stop();
                        }
                    }
                } catch (Exception ex) {

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initData() {
        setDisplayInterface();
        setPageText(mCurrentIndex);
    }

    @Override
    public void onPhotoTap(View view, float v, float v1) {

    }

    @Override
    public void onOutsidePhotoTap() {

    }

    @Override
    public void onClick(View v) {
    }

    public void setPageText(int position) {
    }

    public int getCurrentItem() {
        return mViewPager.getCurrentItem();
    }

    public int getPagerCount() {
        return mPaths.size();
    }

    @Override
    protected void onDestroy() {
        if (mAdapter != null) {
            mAdapter.loadingListener = null;
            mAdapter.progressListener = null;
            mAdapter.photoTapListener = null;
        }
        photoDisplayInterface.clearMemoryCache();
        super.onDestroy();
    }
}
